package com.wanmait.deliverymangementsystem.managecontroller;

import com.wanmait.deliverymangementsystem.pojo.Admin;
import com.wanmait.deliverymangementsystem.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("manage")
public class ManageAdminController {
@Resource
    private AdminService adminService;
@GetMapping("login")
    public void login(Model model,String error){
    if (error!=null)
    {
        model.addAttribute("error","账号或者密码错误");
    }
}

@PostMapping("login")
public String login(Admin admin, HttpSession session){
   Admin loginadmin = adminService.Login(admin);
   if (loginadmin==null){
       return "redirect:/manage/login?error=1";
   }
   else{
       session.setAttribute("admin",loginadmin);
       adminService.updateLastLoginTime(loginadmin.getId());
       return "redirect:/manage/index";

   }
}
@GetMapping("logout")
    public String logout(HttpSession session){
    session.invalidate();
    return "redirect:/manage/login";
}
}
